package com.zk.zklockfororderno;

import org.I0Itec.zkclient.ZkClient;

public abstract class LockAB implements Lock {

    //连接地址
    private static final String ZK_ADDRESS="106.54.34.192:2181";
    //超时时间
    private static final Integer ZK_TIMEOUT=2000;

    protected ZkClient zkClient = new ZkClient(ZK_ADDRESS);
    protected static final String ZKLOCK = "/zklock";


    @Override
    public void getLock() {
        //trulock 判断节点是否已经存在 true 不存在 false 已存在
        if (trylock()){
            System.out.println("");
        }else{
            waitlock();
            getLock();
        }


    }

    protected abstract void waitlock();

    protected abstract boolean trylock();


    @Override
    public void unLock() {
        if (zkClient!=null){
            zkClient.close();
            System.out.println(">>"+"释放锁资源");
        }
    }
}
